package org.dromara.apartment.service.impl;

import org.dromara.apartment.domain.FeeValue;
import org.dromara.apartment.mapper.FeeValueMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.apartment.domain.bo.FeeKeyBo;
import org.dromara.apartment.domain.vo.FeeKeyVo;
import org.dromara.apartment.domain.FeeKey;
import org.dromara.apartment.mapper.FeeKeyMapper;
import org.dromara.apartment.service.IFeeKeyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 杂项费用名称Service业务层处理
 *
 * @author ckj
 * @date 2025-06-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class FeeKeyServiceImpl implements IFeeKeyService {

    private final FeeKeyMapper baseMapper;
    private final FeeValueMapper feeValueMapper;

    /**
     * 查询杂项费用名称
     *
     * @param id 主键
     * @return 杂项费用名称
     */
    @Override
    public FeeKeyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询杂项费用名称列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 杂项费用名称分页列表
     */
    @Override
    public TableDataInfo<FeeKeyVo> queryPageList(FeeKeyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FeeKey> lqw = buildQueryWrapper(bo);
        Page<FeeKeyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的杂项费用名称列表
     *
     * @param bo 查询条件
     * @return 杂项费用名称列表
     */
    @Override
    public List<FeeKeyVo> queryList(FeeKeyBo bo) {
        LambdaQueryWrapper<FeeKey> lqw = buildQueryWrapper(bo);
        List<FeeKeyVo> feeKeyVoList= baseMapper.selectVoList(lqw);
        feeKeyVoList.stream().forEach(feeKeyVo -> {
            feeKeyVo.setFeeValueList(feeValueMapper.selectVoList(new LambdaQueryWrapper<FeeValue>().eq(FeeValue::getFeeKeyId,feeKeyVo.getId())));
        });
        return feeKeyVoList;
    }

    private LambdaQueryWrapper<FeeKey> buildQueryWrapper(FeeKeyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FeeKey> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(FeeKey::getId);
        lqw.like(StringUtils.isNotBlank(bo.getName()), FeeKey::getName, bo.getName());
        lqw.eq(bo.getSort() != null, FeeKey::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增杂项费用名称
     *
     * @param bo 杂项费用名称
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(FeeKeyBo bo) {
        FeeKey add = MapstructUtils.convert(bo, FeeKey.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改杂项费用名称
     *
     * @param bo 杂项费用名称
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(FeeKeyBo bo) {
        FeeKey update = MapstructUtils.convert(bo, FeeKey.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FeeKey entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除杂项费用名称信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
